unit uStreamUtils;
{
Copyright 2009 Sean B. Durkin

This file is part of SBD Crypto Library.
SBD Crypto Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SBD Crypto Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the Lesser GNU General Public License
along with SBD Crypto Library.  If not, see <http://www.gnu.org/licenses/>.
}
interface
uses Classes;

function  CloneMemoryStream( Original: TMemoryStream): TMemoryStream;
procedure BurnMemoryStream( Destructo: TMemoryStream);
procedure BurnMemory( var Buff; BuffLen: integer);
procedure ZeroFillStream( Stream: TMemoryStream);
procedure RandomFillStream( Stream: TMemoryStream);

procedure XOR_Streams2( Dest, Srce: TMemoryStream);  // Dest := Dest xor Srce
procedure XOR_Streams3( Dest, SrceA, SrceB: TMemoryStream);  // Dest := SrceB xor SrceB

implementation









uses Math;

procedure BurnMemory( var Buff; BuffLen: integer);
begin
FillChar( Buff, BuffLen, $FF)
end;


procedure ZeroFillStream( Stream: TMemoryStream);
var
  L: integer;
begin
if assigned( Stream) then
    L := Stream.Size
  else
    L := 0;
if L <= 0 then exit;
FillChar( Stream.Memory^, L, 0)
end;

procedure RandomFillStream( Stream: TMemoryStream);
var
  L, j: integer;
  P: PByte;
  RandomByte: word;
begin
if assigned( Stream) then
    L := Stream.Size
  else
    L := 0;
if L <= 0 then exit;
P := Stream.Memory;
for j := 0 to L - 1 do
  begin
  RandomByte := Random($100);
  P^ := RandomByte;
  Inc( P)
  end
end;


function  CloneMemoryStream( Original: TMemoryStream): TMemoryStream;
var
  L: integer;
begin
if assigned( Original) then
    L := Original.Size
  else
    L := 0;
result := TMemoryStream.Create;
result.Size := L;
if L <= 0 then exit;
Move( Original.Memory^, result.Memory^, L);
result.Position := Original.Position
end;


procedure BurnMemoryStream( Destructo: TMemoryStream);
var
  L: integer;
begin
if not assigned( Destructo) then exit;
L := Destructo.Size;
if L <= 0 then exit;
BurnMemory( Destructo.Memory^, L);
Destructo.Size := 0
end;



procedure XOR_Streams2( Dest, Srce: TMemoryStream);  // Dest := Dest xor Srce
var
  L: integer;
  j: Integer;
  DestP, SrceP: PByte;
begin
if assigned( Dest) and assigned( Srce) then
    L := Min( Dest.Size, Srce.Size)
  else
    L := 0;
if L <= 0 then exit;
DestP := Dest.Memory;
SrceP := Srce.Memory;
for j := 0 to L - 1 do
  begin
  DestP^ := DestP^ xor SrceP^;
  Inc( DestP);
  Inc( SrceP)
  end
end;



procedure XOR_Streams3( Dest, SrceA, SrceB: TMemoryStream);  // Dest := SrceB xor SrceB
var
  L: integer;
  j: Integer;
  DestP, SrceAP, SrceBP: PByte;
begin
if assigned( Dest) and assigned( SrceA) and assigned( SrceB) then
    L := Min( Min( Dest.Size, SrceA.Size), SrceB.Size)
  else
    L := 0;
if L <= 0 then exit;
DestP := Dest.Memory;
SrceAP := SrceA.Memory;
SrceBP := SrceA.Memory;
for j := 0 to L - 1 do
  begin
  DestP^ := SrceAP^ xor SrceBP^;
  Inc( DestP);
  Inc( SrceAP);
  Inc( SrceBP)
  end
end;


end.
